﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeAccessNetdisk.src.classes
{
    public class SingleUserController
    {
        private CloudClient cloudClient;
        private UniversalLogger logger;
        private IWatcher fileWatcher;
        private IWatcher cloudWatcher;
        private IOperator uploader;
        private IOperator downloader;
        private string targetDir;

        public SingleUserController(string userBucket, string targetDir)
        {
            this.targetDir = targetDir;
            logger = UniversalLogger.GetLogger();
            cloudClient = new CloudClient(userBucket, logger);
            fileWatcher = new FileWatcher(targetDir, logger);
            cloudWatcher = new CloudWatcher(cloudClient, targetDir);
            uploader = new Uploader(cloudClient, targetDir);
            downloader = new Downloader(cloudClient, targetDir);
            ResetTarget(targetDir);
        }

        public void Initial()
        {

        }

        public void ResetTarget(string targetDir)
        {
            this.targetDir = targetDir;
            logger.Log(LogType.DASHBOARD, "正在设置本地目标路径...");
            fileWatcher.SetTarget(targetDir);
            cloudWatcher.SetTarget(targetDir);
            uploader.SetTargetDir(targetDir);
            downloader.SetTargetDir(targetDir);
        }

        public void LocalFileChangeHandler(ChangeFileInfo changeFile)
        {
            switch (changeFile.ChangeType)
            {
                case ChangeType.CREATE:
                    logger.Log(LogType.DASHBOARD, $"正在上传 {changeFile.FileDir}");
                    uploader.CreateFile(changeFile);
                    break;
                case ChangeType.MODIFY:
                    logger.Log(LogType.DASHBOARD, $"正在于云端修改 {changeFile.FileDir}");
                    uploader.ChangeFile(changeFile);
                    break;
                case ChangeType.DELETE:
                    logger.Log(LogType.DASHBOARD, $"正在于云端删除 {changeFile.FileDir}");
                    uploader.DeleteFile(changeFile);
                    break;
                default:
                    break;
            }
        }

        public void CloudFileChangeHandler(ChangeFileInfo changeFile)
        {
            switch (changeFile.ChangeType)
            {
                case ChangeType.CREATE:
                    logger.Log(LogType.DASHBOARD, $"正在下载 {changeFile.FileDir}");
                    downloader.CreateFile(changeFile);
                    break;
                case ChangeType.MODIFY:
                    logger.Log(LogType.DASHBOARD, $"正在于本地修改 {changeFile.FileDir}");
                    uploader.ChangeFile(changeFile);
                    break;
                case ChangeType.DELETE:
                    logger.Log(LogType.DASHBOARD, $"正在于本地删除 {changeFile.FileDir}");
                    uploader.DeleteFile(changeFile);
                    break;
                default:
                    break;
            }
        }

        public void StartSync()
        {
            fileWatcher.FileChangeEvent += LocalFileChangeHandler;
            cloudWatcher.FileChangeEvent += CloudFileChangeHandler;
            fileWatcher.StartWatcher();
        }

        public void StopSync()
        {
            fileWatcher.StopWatcher();
        }

        public void CloudSync()
        {
            logger.Log(LogType.CLOUD, "开始从云端同步");
            cloudWatcher.StartWatcher();
            logger.Log(LogType.CLOUD, "从云端同步完成");
        }
    }
}
